<?php
namespace app\common\product\model;


use think\Model;
use think\model\concern\SoftDelete;

class Product extends Model
{
    protected $table = "tk_store_product";

    use SoftDelete;
    protected $defaultSoftDelete = 0;

    protected $type = [
    ];

    public function getCateIdAttr($value)
    {
        return explode(',', $value);
    }

    public function setCateIdAttr($value)
    {
        return implode(',', $value);
    }

    // 获取商品分类
    public function getCategoryAttr($value, $data)
    {
        $model = new StoreCategory();
        $where = [];

        $where[] = ['id', 'in', explode(',', $data['cate_id'])];
        return $model->where($where)->column('id,title');
    }

    public function getSliderImageAttr($value, $data)
    {
        return empty($value) ? [] : json_decode($value, 1);
    }

    public function setSliderImageAttr($value)
    {
        return !empty($this) ? json_encode($value) : '[]';
    }

    public function getFormatSliderImageAttr($value, $data): array
    {
        if (empty($data['slider_image'])) {
            return [];
        }

        $sliderImage = json_decode($data['slider_image'], 1);
        // 获取$data['slider_image']中所有图片的url
        return array_column($sliderImage, 'url');
    }

    public function getSpecAttr($value, $data)
    {
        $model = new ProductAttrValue();
        $where = [];
        $where['product_id'] = $data['id'];
        return $model->where($where)->select();
    }

    public function getSpecListAttr($value, $data): array
    {
        $model = new ProductAttr();
        $where = [];
        $where['product_id'] = $data['id'];
        $attrs = $model->where($where)->select();

        $result = [];
        foreach ($attrs as $item) {
            $specValueList = [];
            $attrValues = explode(',', $item['attr_value']);
            foreach ($attrValues as $attrValue) {
                $specValueList[] = [
                    "specValueId" => '',
                    "specId" => '',
                    "saasId" => '',
                    "specValue" => $attrValue,
                    "image" => '',
                ];
            }

            $result[] = [
                "specId" => '',
                "title" => $item['attr_name'],
                "specValueList" => $specValueList
            ];
        }
        return $result;
    }

    public function getPriceRangeAttr($value, $data): array
    {
        $model = new ProductAttrValue();
        $where = [];
        $where['product_id'] = $data['id'];
        $attrValues = $model->where($where)->column('price,ot_price');

        // 在$attrValues中获取price列的最小值
        $minPrice = min(array_column($attrValues, 'price'));
        $maxPrice = max(array_column($attrValues, 'price'));

        // 在$attrValues中获取ot_price列的最大值
        $minOtPrice = min(array_column($attrValues, 'ot_price'));
        $maxOtPrice = max(array_column($attrValues, 'ot_price'));

        return [
            "price" => $minPrice,
            "otPrice" => $maxPrice,
            'minSalePrice' => $minPrice,
            'maxSalePrice' => $maxPrice,
            'minLinePrice' => $minOtPrice,
            'maxLinePrice' => $maxOtPrice,
        ];
    }

    public function getSkuListAttr($value, $data): array
    {
        $model = new ProductAttrValue();
        $where = [];
        $where['product_id'] = $data['id'];
        $attrValues = $model->where($where)->select();
        $result = [];

        foreach ($attrValues as $item) {
            $specInfo = [];
            $sukIds = explode(',', $item['suk_ids']);
            $sukArr = explode(',', $item['suk']);
            foreach ($sukIds as $key=>$sukId) {
                $specInfo[] = [
                    "specId" => '',
                    "specTitle" => '',
                    "specValueId" => $sukId,
                    "specValue" => $sukArr[$key],
                ];
            }

            $priceInfo = [];
            $priceInfo[] = [
                "priceType" => 1,
                "price" => $item['price'],
                "priceTypeName" => null,
            ];
            $priceInfo[] = [
                "priceType" => 2,
                "price" => $item['ot_price'],
                "priceTypeName" => null,
            ];

            $result[] = [
                "skuId" => $item['unique'],
                "skuImage" => $item['image'],
                "specInfo" => $specInfo,
                "priceInfo" => $priceInfo,
                "stockInfo" => [
                    "stockQuantity" => $item['stock'],
                    "safeStockQuantity" => $item['stock'] - $item['sales'],
                    "soldQuantity" => $item['sales']
                ],
                "weight" => [
                    "value" => $item['weight'],
                    "unit" => 'KG'
                ],
                "volume" => $item['volume'],
                "profitPrice" => 0,
            ];
        }

        return $result;
    }

    public function getDescAttr($value, $data)
    {
        $model = new ProductDescription();
        $where = [];
        $where['product_id'] = $data['id'];
        $row = $model->where($where)->field('description')->find();
        if (empty($row)) {
            return [];
        }

        $row->append(['desc']);

        return $row->desc;
    }
}